package com.rongji.dfish.demo.index.view;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.constant.DemoConstants;
import com.rongji.dfish.demo.editor.view.EditorCommonView;
import com.rongji.dfish.demo.entity.PubLoginUser;
import com.rongji.dfish.demo.index.service.AnchorHelper;
import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.command.JSCommand;
import com.rongji.dfish.ui.form.Password;
import com.rongji.dfish.ui.form.Text;
import com.rongji.dfish.ui.form.Validate;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.layout.ButtonBar;
import com.rongji.dfish.ui.layout.EmbedWindow;
import com.rongji.dfish.ui.layout.FrameLayout;
import com.rongji.dfish.ui.layout.HorizontalLayout;
import com.rongji.dfish.ui.layout.VerticalLayout;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.Button;
import com.rongji.dfish.ui.widget.Html;
import com.rongji.dfish.ui.widget.SubmitButton;

public class DemoView extends EditorCommonView {

	public static View buildIndexView(PubLoginUser loginUser,String anchor) {
		if(anchor==null){
			anchor="";
		}
		View view = new View();
		
		VerticalLayout root=new VerticalLayout("root").setCls("bg-index");
		view.setNode(root);
		
		HorizontalLayout top=new HorizontalLayout("top").setCls("bg-top");
		root.add(top, "75");
		
		top.add(new Html(null,"&nbsp; <i class=ico-logo></i>").setStyle("margin-left:30px;").setWmin(30).setValign("middle"), "200");
		
		ButtonBar wstab=new ButtonBar("wstab").setAlign(ButtonBar.ALIGN_RIGHT);
		top.add(wstab, "*");
		wstab.getPub().setCls("wstab").setFocusable(true);
		
//		//标签页
//		List<Button> buttons=new ArrayList<Button>();
//		
//		buttons.add(new Button("首页").setTarget("mod_"+DemoConstants.MOD_INDEX));
//		buttons.add(new Button("示例").setTarget("mod_"+DemoConstants.MOD_EXAMPLE));
//		buttons.add(new Button("体验").setTarget("mod_"+DemoConstants.MOD_EXPERIENCE));
//		buttons.add(new Button("文档").setTarget("mod_"+DemoConstants.MOD_DOCUMENT));
//		buttons.add(new Button("下载").setTarget("mod_"+DemoConstants.MOD_DOWNLOAD));
//		buttons.add(new Button("社区").setTarget("mod_"+DemoConstants.MOD_COMMUNITY));
//		String focusMod="mod_"+DemoConstants.MOD_INDEX;
//		if(Utils.notEmpty(AnchorHelper.getInstance().getModleKey(anchor))){
//			focusMod="mod_"+AnchorHelper.getInstance().getModleKey(anchor);
//		}
//		for(Button btn:buttons){
//			if(btn.getTarget().equals(focusMod)){
//				btn.setFocus(true);
//			}
//			wstab.add(btn);
//		}
		
		
		ButtonBar loginBar = buildLoginBar(loginUser);
		top.add(loginBar, "92");
		
		FrameLayout main=new FrameLayout("main");
		root.add(main, "*");
		// FIXME 目前开发阶段聚焦在这里,正式对外时默认聚焦第一个
//		main.setDft(focusMod);
		
		String[][] viewData=new String[][]{
				{DemoConstants.MOD_INDEX, "首页", "index.html"},
				{DemoConstants.MOD_EXAMPLE, "示例", "editorDir/index?fromModule="+DemoConstants.MOD_EXAMPLE},
				{DemoConstants.MOD_EXPERIENCE, "体验",  "editorDir/index?fromModule="+DemoConstants.MOD_EXPERIENCE},
				{DemoConstants.MOD_DOCUMENT, "文档", "intro/index"},
				{DemoConstants.MOD_DOWNLOAD, "下载", "intro/download"},
				{DemoConstants.MOD_COMMUNITY, "社区", "comm/index?plateId="},
		};
		
		
		String focusMod="mod_"+DemoConstants.MOD_INDEX;
		if(Utils.notEmpty(AnchorHelper.getInstance().getModleKey(anchor))){
			focusMod="mod_"+AnchorHelper.getInstance().getModleKey(anchor);
		}
		for(String[] row : viewData){
			String key=row[0];
			String text=row[1];
			String url=row[2];
			
			String modId = "mod_"+key;
			Button btn = new Button(text);
			if(DemoConstants.MOD_INDEX.equals(key)){
				btn.setOn(Button.EVENT_CLICK, "location.href='index.html';");
			} else {
				btn.setTarget(modId);
			}
			wstab.add(btn);
			if (modId.equals(focusMod)) {
				btn.setFocus(true);
			}
			if (DemoConstants.MOD_INDEX.equals(key)) {
				EmbedWindow win = new EmbedWindow(modId, url);
				main.add(win);
			} else {
				if(Utils.notEmpty(anchor)&&key.equals(AnchorHelper.getInstance().getModleKey(anchor))){
					main.add(new View(modId).setSrc(url + (url.indexOf('?')>=0?"&":"?")+"anchor="+anchor));
				}else{
					main.add(new View(modId).setSrc(url));
				}
			}
		}
		main.setDft(focusMod);
		
		view.addTemplate("std", PubView.getStdTemplate());
		view.addTemplate("alert", PubView.getAlertTemplate());
		view.addTemplate("prong", PubView.getProngTemplate());
		
		return view;
				
	}
	
	public static ButtonBar buildLoginBar(PubLoginUser loginUser) {
		ButtonBar loginBar = new ButtonBar("loginBar").setStyle("padding:0 5px;color:#FFF;").setWmin(10);
		loginBar.getPub().setCls("f-button-link log-user-btn");
//		loginBar.setSplit(new Split("|").setWidth(10));
		if (loginUser == null) {
			loginBar.add(new Button(".user-profile", "登录").setOn(Button.EVENT_CLICK, "this.cmd({type:'ajax',src:'login/toLogin'});"));
		} else {
			Button btnLogin = new Button(".user-profile", null).setHoverdrop(true).setWidth(70);
			loginBar.add(btnLogin);
			btnLogin.add(new Button("个人信息").setOn(Button.EVENT_CLICK, "this.cmd({type:'ajax',src:'demo/editUser'});"));
			btnLogin.add(new Button("密码修改").setOn(Button.EVENT_CLICK, "this.cmd({type:'ajax',src:'demo/editPwd'});"));
			btnLogin.add(new Button("安全退出").setOn(Button.EVENT_CLICK, "this.cmd({type:'ajax',src:'login/logout'});"));
		}
//		loginBar.add(new Button("帮助"));
		return loginBar;
	}

	public static View buildEditUserView(PubLoginUser loginUser) {
		View view = PubView.buildDialogView();
		
		FormPanel main = (FormPanel) view.findNodeById(PubView.DIALOG_MAIN);
		main.add(new Text("loginName", "账号", loginUser.getLoginName()).setDisabled(true));
		main.add(new Text("userName", "姓名", loginUser.getUserName()).setRequired(true));
		main.add(new Text("userEmail", "邮箱", loginUser.getUserEmail()).setRequired(true)
					.addValidate(new Validate().setPattern("/^[a-zA-Z0-9_-]+@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}/")));
//				.setValidate(new Validate().setPattern("/^[a-zA-Z0-9_-]+@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}/")));
		
		ButtonBar btnBar = (ButtonBar) view.findNodeById(PubView.DIALOG_BTN);
		btnBar.add(0, new SubmitButton("确定").setOn(Button.EVENT_CLICK, "this.cmd('save');"));
		
		view.addCommand("save", PubView.getSubmitCommand("demo/saveUser"));
		return view;
	}

	public static Widget<?> buildEditPwdView() {
		View view = PubView.buildDialogView();
		
		FormPanel main = (FormPanel) view.findNodeById(PubView.DIALOG_MAIN);
		main.add(new Password("oldPwd", "输入旧密码", null).setId("oldPwd").setRequired(true));
		main.add(new Password("password", "输入新密码", null).setRequired(true));
		main.add(new Password("cfmPassword", "确认新密码", null).setRequired(true).setOn(Text.EVENT_BLUR, "demo.checkPwd(this);"));
		
		ButtonBar btnBar = (ButtonBar) view.findNodeById(PubView.DIALOG_BTN);
		btnBar.add(0, new SubmitButton("确定").setOn(Button.EVENT_CLICK, "demo.checkPwd(this,'1');"));
		
		view.addCommand("save", PubView.getSubmitCommand("demo/savePwd"));
		return view;
	}
	/**
	 * 消息提示框(为了解决首页html冲突的问题，特殊的弹出框)
	 * @param alertInfo 消息内容
	 * @return
	 */
	public static JSCommand getAlertInfo(String alertInfo) {
		JSCommand alert=new JSCommand("dfish.alert('"+alertInfo+"',5,5);");
		return alert;
	}
}
